<?php

/**
 * PictureController
 * 
 * @author
 * @version 
 */

require_once 'Zend/Controller/Action.php';

class PictureController extends Zend_Controller_Action 
{
	protected $_pictureModel;
	
	public function init()
	{
		$this->_pictureModel = new Eleven_Model_Picture();
		
		$this->view->uploadForm = $this->getUploadForm();
	}
	
	public function indexAction() 
	{
    
	    $catepics = $this->_pictureModel->getCatepics();
        $this->view->assign(array('catepics'=>$catepics));    
	            
	}
	
	public function catelistAction()
	{
	    $currentCateId = $this->_request->getParam('cateId',null);
	    
	    if ($currentCateId)
	    {
	        $pictures = $this->_pictureModel->getPictures($currentCateId, $this->_getParam ( 'page', 1 ), array ('id DESC' ));
	        $this->view->assign(array('pictures'=>$pictures));
	    }
	}
	
	public function uploadAction()
	{
	    //Zend_Debug::dump($this->_pictureModel->getIdentity());exit;
	    if (!$this->_pictureModel->checkAcl('upload')) {
            return $this->_helper->redirector('index','picture');
        }
	}
	
	public function saveAction()
	{
		$request = $this->getRequest();
		if (!$request->isPost()) 
		{
			return $this->_helper->redirector('index');
  		}
  		
  		if (false === $this->_pictureModel->savePicture($request->getPost())) 
  		{
     		return $this->render('upload');
  		}
  		
  		return $this->_helper->redirector('index');
	}
	
	public function getUploadForm()
	{
		$urlHelper = $this->_helper->getHelper('url');

        $this->_forms['post'] = $this->_pictureModel->getForm('pictureUpload');
        $this->_forms['post']->setAction($urlHelper->url(array(
            'action' => 'save'
            ),
            'default'
        ));
       
        $this->_forms['post']->setMethod('post');

        return $this->_forms['post'];
        
	}

}

